package com.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/loginCheck")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //获取数据
        String uname = req.getParameter("username");
        String pwd = req.getParameter("passpwd");
        PrintWriter out = resp.getWriter();

        String msg="";
        //如何操作
        if (uname !=null && uname.length()>0 && pwd != null && pwd.length()>0) {
            if(uname.equals("abc") && pwd.equals("123")) {
//                out.println(uname + ":欢迎你，恭喜你登陆成功");
                msg = uname + ":欢迎你，恭喜你登陆成功999999999999999";
                req.setAttribute("msg",msg);
                req.getRequestDispatcher("main.jsp").forward(req,resp);
            } else {
//                out.println("用户名或者密码不正确");
                resp.sendRedirect("http://www.baidu.com");
            }
        } else {
            out.println("用户名或密码为空");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
